package com.chj.comp.common.help.util;

import org.jasypt.util.text.BasicTextEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

/**
 * @author ：cuihui
 * @date ：Created in 2022/1/6
 * @description：配置文件加解密
 * @modified By：
 */
public class JasyptUtil {

    /**
     * 秘钥
     */
//    private final static String KEY = "Blmp@2022!";
    private final static String KEY = "Scbb@2022!";
//    private final static String KEY = "comp";

    private final static String ALGORITHM = "PBEWithMD5AndDES";

    private static BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();

    private static StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();

    static {
        basicTextEncryptor.setPassword(KEY);

        standardPBEStringEncryptor.setPassword(KEY);
        standardPBEStringEncryptor.setAlgorithm(ALGORITHM);
    }

    /**
     *  加密（基本）
     * @param text 待加密字符串
     * @return
     */
    public static String encryptStrByBasic(String text) {
        String encryptedText = basicTextEncryptor.encrypt(text);
        return encryptedText;
    }

    /**
     *  加密（标准）
     * @param text 待加密字符串
     * @return
     */
    public static String encryptStrByStandard(String text) {
        String encryptedText = standardPBEStringEncryptor.encrypt(text);
        return encryptedText;
    }

    /**
     *  解密（基本）
     * @param text 加密后的字符串
     * @return
     */
    public static String decryptStrByBasic(String text) {
        String plainText = basicTextEncryptor.decrypt(text);
        return plainText;
    }

    /**
     *  解密（标准）
     * @param text 加密后的字符串
     * @return
     */
    public static String decryptStrByStandard(String text) {
        String plainText = standardPBEStringEncryptor.decrypt(text);
        return plainText;
    }

    public static void main(String[] args) {
        /**
         * 基本加解密
         */
        System.out.println("----------基本加解密----------");
        // 加密
        System.out.println(JasyptUtil.encryptStrByBasic("root"));
        System.out.println(JasyptUtil.encryptStrByBasic("Plkbdb_2022"));
        // 解密
        System.out.println(JasyptUtil.decryptStrByBasic("YCbuISdTiRFTBRkQgPFOyg=="));
        System.out.println(JasyptUtil.decryptStrByBasic("H3McDAbPOYcXD9SrLHPxkjguTuqpxgyW"));
        System.out.println("----------基本加解密----------");

        /**
         * 标准加解密
         */
        System.out.println("----------标准加解密----------");
        // 加密
        System.out.println(JasyptUtil.encryptStrByStandard("root"));
        System.out.println(JasyptUtil.encryptStrByStandard("p9nk28vgtnhk0dim"));
        // 解密
        System.out.println(JasyptUtil.decryptStrByStandard("IcoUX+cXzR+uQHiPeH/YvFwT2RdMIgbOrySu344Cu4I="));
        System.out.println(JasyptUtil.decryptStrByStandard("PDJIkl1PamA534glHJRaR9dWzJI+FaMWpejnAiCJlbV1wYRkrQHfBiOXsTLUC0aw"));
        System.out.println("----------标准加解密----------");
    }
}
